#include <iostream>
#include <cerrno>
#include <sys/socket.h>
#include <sys/types.h>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>
void usage(std::string proc)
{
    std::cout<<"usage: \n\t"<<proc<<" server ip server port"<<std::endl;
}
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        usage(argv[0]);
        return 0;
    }


    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        std::cerr << "sock creat error" << errno << std::endl;
        return 1;
    }

    sockaddr_in server;
    server.sin_family=AF_INET;
    server.sin_port=htons(atoi(argv[2]));
    server.sin_addr.s_addr=inet_addr(argv[1]);
    while (1)
    {
        std::string str;
        std::cout<<"输入# ";
        std::cin >> str;
        
        sendto(sock,str.c_str(),str.size(),0,(struct sockaddr*)&server,sizeof(server));

        struct sockaddr_in tmp;
        socklen_t len=sizeof(tmp);
        char buf[1024];
        recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr*)&tmp,&len);
        std::cout<<"server echo#"<<buf<<std::endl;

    }

    return 0;
}